package com.ukao.pad.ui.collectMoney;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.ukao.pad.R;
import com.ukao.pad.adapter.PayTagAdapter;
import com.ukao.pad.base.BaseFragment;
import com.ukao.pad.bean.BalanceBean;
import com.ukao.pad.bean.PayInfoBean;
import com.ukao.pad.consts.Constant;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.eventbus.PayEvent;
import com.ukao.pad.retrofit.ApiCallback;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.T;
import com.ukao.pad.utils.Utils;
import com.ukao.pad.widget.StateButton;
import com.ukao.pad.widget.TagFlowLayout;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.Observable;

import static com.ukao.pad.eventbus.PayEvent.Message.PAY_SUCCEED;

/**
 * 其他  银行卡
 */
public class UnionpayFragment extends BaseFragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.should_Price)
    TextView shouldPrice;
    @BindView(R.id.remark)
    EditText remark;
    @BindView(R.id.sure_gather)
    StateButton sureGather;
    @BindView(R.id.id_tagflowlayout)
    TagFlowLayout mTagFlowLayout;  // 显示银行卡标签的布局
    private Unbinder unbinder;
    private PayTagAdapter mPayTagAdapter;
    private String shouldPice = "0";
    private PayInfoBean mPayInfo;


    public static UnionpayFragment newInstance(PayInfoBean mPayInfo) {
        UnionpayFragment fragment = new UnionpayFragment();
        Bundle args = new Bundle();
        args.putParcelable(ARG_PARAM1, mPayInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPayInfo = getArguments().getParcelable(ARG_PARAM1);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_unionpay, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    protected void initView() {
        setGatheringMoney();
    }

    /**
     * 设置收款金额
     */
    public   void  setGatheringMoney(){

        shouldPice = CheckUtils.isEmptyNumber(mPayInfo.getShouldPrice());
        if (shouldPice.indexOf(".") != -1) {
            Spannable span = new SpannableString(shouldPice);
            span.setSpan(new AbsoluteSizeSpan(CheckUtils.isMIUIDevices()?45:60),shouldPice.indexOf("."),shouldPice.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            shouldPrice.setText(span);
        } else {
            shouldPrice.setText(shouldPice);
        }
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        fillpaySignData(mPayInfo);
    }

    public  void  fillpaySignData(PayInfoBean  mPayInfo){
        if (mPayInfo!=null&&mPayInfo.isEnableOther()&&mPayTagAdapter==null){
            mPayTagAdapter= new PayTagAdapter(activity, mPayInfo.getPaySignList());
            mTagFlowLayout.setAdapter(mPayTagAdapter);
        }
    }

    @OnClick(R.id.sure_gather)
    public void onViewClicked() {
        unionPay();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
   private String paySign="";
    public void unionPay() {
        showProgressDialog();
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        if (mPayTagAdapter!=null&&mPayTagAdapter.getCount()!=0){
            int selectPos =-1;
            for (Integer pos : mTagFlowLayout.getSelectedList()) {
                selectPos =pos;
            }
            if (selectPos !=-1){
                PayInfoBean.PaySignListBean paySignListBean = mPayTagAdapter.getItem(selectPos);
                paySign= paySignListBean.getVal();
            }
        }
        mHashMap.put("orderId", mPayInfo.getOrderId());
        mHashMap.put("remark", getText(remark));  //输入的备注
        if (!CheckUtils.isEmpty(mPayInfo.getCpnRelId())) {  /*优惠券id*/
            mHashMap.put("cpnRelId", mPayInfo.getCpnRelId());
        }
        if (!CheckUtils.isEmpty(paySign)){  /*银行卡选项*/
            mHashMap.put("paySign", paySign);
        }
        if (mPayInfo.isUseUPay()) {
            mHashMap.put("groupCardType","41");//groupCardType 支付(团)卡类型 41-优付卡
        } else {
            mHashMap.put("balance",String.valueOf(mPayInfo.getPayBalance()));
        }
        Observable   observable = apiStores().otherPay(Constant.createParameter(mHashMap));
        addSubscription(observable, new ApiCallback<BalanceBean>() {
            @Override
            public void onSuccess(BalanceBean model) {
                if (model.getHttpCode() == 200) {
                    PayEvent.postNoData(PAY_SUCCEED);
                } else {
                    T.show(model.getMsg());
                }
            }
            @Override
            public void onNoNewWork() {

            }


            @Override
            public void onFinish() {
                dismissProgressDialog();
            }

        });
    }
}
